<?php
defined('GMANAGER_LIBS') or die('You can not access the page.');

/**
 *  
 *  Doctrine 需要 PHP 5.3.x 以上的版本
 *  
 *  下載:
 *      http://www.doctrine-project.org/projects/orm
 *
 *  pear:
 *      sudo pear channel-discover pear.doctrine-project.org
 *      sudo pear install -o pear.doctrine-project.org/DoctrineCommon
 *      sudo pear install -o pear.doctrine-project.org/DoctrineORM
 *
 *      pear list -c pear.doctrine-project.org
 *      ls -la /usr/share/php/Doctrine
 *
 */
//require_once GMANAGER_LIBS.'/doctrine2/libs/lib/Doctrine.php';

require_once 'Doctrine/Common/ClassLoader.php';

class Gmanager_Doctrine2
{
    public function get( $appendConfig=NULL )
    {

        $config = self::getConfig();
        if( is_array($appendConfig) ) {
            $config = $appendConfig + $config;
        }

        $env = 'development';


        $classLoader = new \Doctrine\Common\ClassLoader('Doctrine');
        $classLoader->register();

        $classLoader = new \Doctrine\Common\ClassLoader('Entities');    //, __DIR__
        $classLoader->register();

        $classLoader = new \Doctrine\Common\ClassLoader('Proxies');     //, __DIR__
        $classLoader->register();


        $cf = new \Doctrine\ORM\Configuration();
        $cf->setProxyDir( $config['proxy_directory'] );
        $cf->setProxyNamespace('Proxies');
        if( $env == "development" ) {
            $cf->setAutoGenerateProxyClasses(true);
        }

        
      //$driverImpl = new \Doctrine\ORM\Mapping\Driver\YamlDriver($config['yaml_directory']);   // 建立 yaml 使用
        $driverImpl = $cf->newDefaultAnnotationDriver( $config['entities_directory'] );         // 讀取 entities 使用
        $cf->setMetadataDriverImpl( $driverImpl );


        // 以下是建議的屬性:
        if( $env == "development" ) {
            $cache = new \Doctrine\Common\Cache\ArrayCache();
        }
        else {
            $cache = new \Doctrine\Common\Cache\ApcCache();
        }
        $cf->setMetadataCacheImpl($cache);
        $cf->setQueryCacheImpl($cache);


        $em = \Doctrine\ORM\EntityManager::create( $config['connection_options'] , $cf);
        return $em;

    }

    public function getConfig()
    {
        return include GMANAGER_LIBS.'/doctrine2/config.inc.php';
    }

}

